﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewsListDialog.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the ViewsListDialog type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows.Forms;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the views list dialog.
  /// </summary>
  public partial class ViewsListDialog: ListDialogBase
  {
    #region Constants and Fields


    private readonly ApplicationViewCollection _applicationViews;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="ViewsListDialog"/> class.
    /// </summary>
    public ViewsListDialog(ApplicationViewCollection applicationViews)
    {
      _applicationViews = applicationViews;
      InitializeComponent();

      ListView = viewsList;
    }


    #endregion


    #region Methods


    /// <summary>
    /// Gets the list view item.
    /// </summary>
    /// <param name="item">
    /// The item.
    /// </param>
    /// <returns>
    /// </returns>
    protected override string GetListViewItemText(object item)
    {
      return ((ApplicationView)item).Label;
    }


    /// <summary>
    /// Gets the sorted items list.
    /// </summary>
    /// <returns>
    /// </returns>
    protected override List<object> GetSortedItemsList()
    {
      var views = new List<ApplicationView>(_applicationViews);

      views.Sort((view1, view2) => string.Compare(view1.Label, view2.Label));

      return new List<object>(views.Cast<object>());
    }


    /// <summary>
    /// Updates the controls.
    /// </summary>
    protected override void UpdateControls()
    {
      removeMenuItem.Enabled = FocusedListViewItem != null;
    }


    private void AddMenuItemClick(object sender, EventArgs e)
    {
      var dialog = new EditViewDialog();

      ShowEditDialog(dialog);
    }


    private void CloseMenuItemClick(object sender, EventArgs e)
    {
      Close();
    }


    private void RemoveMenuItemClick(object sender, EventArgs e)
    {
      string text = string.Format("Are you sure you want to remove view '{0}'?", ((ApplicationView)FocusedItem).Label);

      DialogResult result = MessageBox.Show(text, "Remove View?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, 
                                            MessageBoxDefaultButton.Button1);

      if (result != DialogResult.OK)
      {
        return;
      }

      _applicationViews.Remove((ApplicationView)FocusedItem);
      RemoveItem(FocusedListViewItem);
    }


    private void ShowEditDialog(EditViewDialog dialog)
    {
      DialogResult result = dialog.ShowDialog();

      if (result != DialogResult.OK)
      {
        return;
      }

      if (_applicationViews.Contains(dialog.ViewName))
      {
        MessageBox.Show("A View with the same name already exists.", "Invalid View Name", MessageBoxButtons.OK,
                        MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);

        ShowEditDialog(dialog);

        return;
      }

      var view = new ApplicationView();
      view.ClassName = dialog.ViewClass.Name;
      view.Label = dialog.ViewName;
      view.IsDefault = !_applicationViews.Any();

      _applicationViews.Add(view);
      InsertItem(view);
    }


    #endregion
  }
}